package com.shadow.study.jpa.controller;

import com.shadow.study.jpa.entity.JPARoleEntity;
import com.shadow.study.jpa.service.JPARoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("role")
public class JPARoleController {

    @Resource
    private JPARoleService roleService;

    @GetMapping("get")
    public Object get() {
        return roleService.findByName("系统管理员");
    }

    @GetMapping("like/{code}")
    public Object like(@PathVariable("code") String code) {
        return roleService.findByCodeLike(code);
    }

    @GetMapping("page")
    public Object page() {
        return roleService.findAll(PageRequest.of(0,2));
    }

    @GetMapping("add")
    public Object add() {
        JPARoleEntity role = new JPARoleEntity();
        role.setCode("1")
                .setName("test")
                .setDescription("xxx")
                .setStatus(1)
                .setTestExist("test");
        return roleService.save(role);
    }

    @GetMapping("delete/{id}")
    public void delete(@PathVariable("id") String id) {
        roleService.delete(id);
    }
}
